<?php
class AdsBannersController extends LilAdsAppController {
    var $name = 'AdsBanners';
	var $uses = array('LilAds.AdsBanner', 'LilAds.AdsCampaign');
/**
 * beforeFilter method
 *
 * @access public
 * @return void
 */
	function beforeFilter() {
		parent::beforeFilter();
		if (!empty($this->Auth)) $this->Auth->allow('click', 'display');
	}
/**
 * isAuthorized method
 *
 * @access public
 * @return bool
 */
	function isAuthorized() {
		if (!parent::isAuthorized()) return false;
		
		// plugins can also change authorization
		$c = $this->callPluginHandlers('isAuthorizedLilAds', array('return' => true, 'continue' => true));
		if (!isset($c['return']) || $c['return'] == false) return false;
		
		// plugin can determine if it wants to end authorization check
		if (!isset($c['continue']) || $c['continue'] == true) {
			if (in_array($this->params['action'], array('click', 'display'))) return true;

			return $this->isAdmin();
		}
		return true;
	}
	
	function click($id=null) {
		if (!is_numeric($id)) {
			$this->cakeError('error404');
		}
		$redirect = $this->AdsBanner->click($id);
		if (!empty($this->params['named']['link'])) {
			$redirect = urldecode($this->params['named']['link']);
			if (substr($redirect, 0, 4) != 'http') $redirect = 'http://'.$redirect;
		}
		if (!empty($redirect)) {
			$this->redirect($redirect);
		} else {
			$this->cakeError('error404');
		}
	}
	
	function display($id=null) {
		if (is_numeric($id) && $banner = $this->AdsBanner->read(null, $id)) {
			$this->layout = false;
			Configure::write('debug', 0);
			$this->autoRender = false;
			
			$filename = Configure::read('Lilads.uploadFolder') . $banner['AdsBanner']['filename'];
			switch ($banner['Banner']['kind']) {
				case 'I':
					ob_end_clean();
					$content_types = array('gif'=>'image/gif', 'png'=>'image/png', 'jpg'=>'image/jpeg', 'jpeg'=>'image/jpeg');
					header('Content-Transfer-Encoding: binary');
					header('Last-Modified: '.date('r'));
					header('Content-Length: '.filesize($filename));
					header('Content-Type: '.$content_types[strtolower(substr($filename, strrpos($filename, '.') + 1))]);
					
					readfile($filename);
					exit;
					break;
				case 'F':
					ob_end_clean();
					header('Content-Transfer-Encoding: binary');
					header('Last-Modified: ' . date('r'));
					header('Content-Type: application/x-shockwave-flash');
					header('Content-Length: ' . filesize($filename));
					readfile($filename);
					exit;
					break;
				case 'P':
					echo h($banner['AdsBanner']['body']);
					break;
				case 'H':
					echo $banner['AdsBanner']['body'];
					break;
			}
		} else {
			$this->cakeError('error404');
		}
	}

	function admin_add($id=null) {
		$this->setAction('admin_edit');
	}

	function admin_edit($id = null) {
		if (!empty($this->data)) {
			if ($this->AdsBanner->save($this->data)) {
				$this->Session->setFlash(__('Banner has been saved.', true));
				
				if (!empty($this->data['AdsBanner']['referer'])) {
					$this->redirect($this->data['AdsBanner']['referer']);
				} else {
					$this->redirect(array('action' => 'index'));
				}
			} else {
				$this->Session->setFlash(__('Please verify that the information is correct.', true), null, null, 'error');
			}
		}
		if (empty($this->data)) {
			if (is_numeric($id)) {
				$this->data = $this->AdsBanner->read(null, $id);
			} else {
				if (!empty($this->params['named']['campaign']) && is_numeric($this->params['named']['campaign'])) {
					$this->data['AdsBanner']['campaign_id'] = $this->params['named']['campaign'];
				} else {
					$campaigns = $this->AdsCampaign->find('list', array(
						'conditions' => array('active' => 1),
						'order'      => 'title'
					));
					$this->set(compact('campaigns'));
				}
				$this->data['AdsBanner']['kind'] = 'I';
			}
			$this->data['AdsBanner']['referer'] = $this->referer();
		}
	}

	function admin_delete($id = null) {
		if (is_numeric($id) && $this->AdsBanner->delete($id)) {
			$this->Session->setFlash(__('Ad has been deleted.', true));
			$this->redirect($this->referer());
		} else {
			$this->error404();
		}
	}
}
?>